package com.yubest.junit.lambda;

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;

/**
 * 方法处理，帮助类
 *
 * @Author: hweiyu
 * @Date: 2022/10/28 10:11
 */
public class MethodHolder {

    public static <T> String getMethodName(MethodFunction<T, String> func) {
        try {
            //实现Serializable接口时，才会有 writeReplace() 方法
            Method method = func.getClass().getDeclaredMethod("writeReplace");
            method.setAccessible(Boolean.TRUE);
            SerializedLambda lambda = (SerializedLambda) method.invoke(func);
            return lambda.getImplMethodName();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
